--[[
@classmod Screen
]]

local Input = require "input"
local Camera = require (LIB_PATH.."camera")

return pl.class {
	_name = "Screen",
	_init = function (self, w, h, scale)
		self.w, self.h = w, h
		self.scale = scale or 4.5
		self.wide_scale = self.scale
		self.focus_scale = 55
		self:reload_camera ()
		self:center(0, 0, w, h)
	end,
	
	reload_camera = function (self)
		self.camera = Camera.new(_lg.getWidth(), _lg.getHeight(), {
			resizable = true, 
			maintainAspectRatio = true,
			translationX = 0, 
			translationY = 0, 
			scale = self.scale
		})
		self:center(0, 0, self.w, self.h)
	end,
	
	center = function (self, x, y, w, h)
		self.camera:setTranslation (x+w/2, y+h/2)
	end,
	
	resize = function (self, w, h)
		self:reload_camera()
	end,
	
	get_mouse_position = function (self)
		return self.camera:getMouseWorldCoordinates()
	end,
	
	update = function (self, dt)
		self.camera:update()
	end,
	
	draw  = function (self, fdl)
		self.camera:push()
			pl.tablex.map(function (v) v:draw() end, fdl)
		self.camera:pop()
		
	end,
	
	to_local = function (self, x, y)
		return self.camera:getWorldCoordinates(x, y)
	end,
	
	to_screen = function (self, x, y)
		return self.camera:getScreenCoordinates(x, y)
	end,
	
	focus_on = function (self, x, y, w, h)
		self:center(x, y, w, h)
		self.camera:setScale (70)
		Sled.state = "focused"
		Sled.grid:resize(Sled.tile_size, Sled.tile_size)
		Sled.grid:move(Sled.grid.tx, Sled.grid.ty)
	end,
	
	unfocus = function (self)
		self:center(0, 0, self.w, self.h)
		self.camera:setScale(4.5)
		Sled.state = "map"
		Sled.current_tile:copy()
	end,
	
	zoom_on_mouse_by = function (self, z)
		local zoom = z * self.camera:getScale() * 5
		self.camera:increaseScaleToPoint(zoom)
		self.scale = self.camera:getScale()
	end,
	
	
}
